﻿//    TestGenerator - experiment with piping data into ffmpeg in "pipefmt"
//    Copyright (C) 2012  Juergen Bohl

//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.

//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.

//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.

namespace TestGenerator
{
    #region using declarations
    using System;
    using System.Collections.Generic;
    using System.Text;
    #endregion

    internal class PipeFormatHelper
    {
        /// <summary>
        /// The frameheader magic ("PIPEFORMAT\n").
        /// </summary>
        private static readonly byte[] FileHeaderMagic = { 80, 73, 80, 69, 70, 79, 82, 77, 65, 84, 10 };

        /// <summary>   
        /// Create the header declaration for the ffmpeg-pipefmt.
        /// </summary>
        /// <param name="writeFunc">    The function which will be called with the generated header. </param>
        /// <param name="videoStreams"> The video streams. </param>
        /// <param name="audioStreams"> The audio streams. </param>
        public static void WriteHeader(Func<byte[], bool> writeFunc, IEnumerable<VideoFormat> videoStreams, IEnumerable<AudioFormat> audioStreams)
        {
            bool b = writeFunc(FileHeaderMagic);
            StringBuilder sb = new StringBuilder();
            if (videoStreams != null)
            {
                foreach (var videoFormat in videoStreams)
                {
                    PipeFormatHelper.VideoFormatToPipeFormatHeader(videoFormat, sb);
                }
            }

            if (audioStreams != null)
            {
                foreach (var audioFormat in audioStreams)
                {
                    PipeFormatHelper.AudioFormatToPipeFormatHeader(audioFormat, sb);
                }
            }

            sb.Append("END:\n");
            byte[] fmtAscii = Encoding.ASCII.GetBytes(sb.ToString());
            writeFunc(fmtAscii);
        }

        private static void VideoFormatToPipeFormatHeader(VideoFormat videoFormat, StringBuilder sb)
        {
            sb.AppendFormat("VSTREAM:W={0} H={1} F=25:1 A=1:1\n", videoFormat.Width, videoFormat.Height);
        }

        private static void AudioFormatToPipeFormatHeader(AudioFormat audioFormat, StringBuilder sb)
        {
            sb.AppendFormat("ASTREAM:R={0} C={1} F={2}\n", audioFormat.SampleRate, audioFormat.ChannelCount, audioFormat.SampleType.ToString());
        }
    }
}
